/*
 * @Author: zhouzhishou
 * @Date: 2022-07-15 20:53:33
 * @Description: 
 */
/**
 * @param {string[]} tokens
 * @return {number}
 */

let operation = {
    '+': (a:number,b:number)=>a+b,
    '-': (a:number,b:number)=>a-b,
    '*': (a:number,b:number)=>a*b,
    '/': (a:number,b:number)=>parseInt( a/b + '')
}

function isOperator(str:string){
    return ['+','-','*','/'].includes(str)
}

var evalRPN = function (tokens: string[]): number {
    if(tokens===null || tokens.length ===0) return 0
    let stack: string[] = []
    while(tokens.length){
        //从左至右扫描tokens
        let str = tokens.shift()
        if(!isOperator(str)){
            stack.push(str)
        }else {
            let num1:number = parseInt(stack.pop())
            let num2:number = parseInt(stack.pop())
            let result = operation[str].call(null,num2,num1)
            stack.push(result)
        }
    }
    return parseInt(stack.pop())
};

// let tokens = ["2","1","+","3","*"]
// let tokens = ["4","13","5","/","+"]
// let tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]

// console.log(evalRPN(tokens));